import { Stack } from "expo-router";
import { StatusBar } from "react-native";
import "../global.css";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import { Provider } from "react-redux";
import store, { persistor } from "../store";
import { ToastAPI, withToast } from "@/components//Toast";
import { ActionSheetProvider } from "@expo/react-native-action-sheet";
import useAppStateChange from "@/hooks/useAppStateChange";
import { useFonts } from "expo-font";
import { Provider as AntdProvider } from "@ant-design/react-native";
import { PersistGate } from "redux-persist/integration/react";
import {
  requestNotificationPermission,
  configureNotificationChannel,
  scheduleLocalNotification,
} from "@/hooks/notifications";
import { useEffect } from "react";
import * as Network from "expo-network";
// 导入i18n配置
import "../i18n";
import { useTranslation } from "react-i18next";

function RootLayout() {
  const appState = useAppStateChange();
  const { t } = useTranslation();
  const [fontsLoaded] = useFonts({
    antoutline: require("@ant-design/icons-react-native/fonts/antoutline.ttf"),
  });
  useEffect(() => {
    const checkNetworkStatus = async () => {
      // 移除权限请求，直接监听网络状态
      const handleNetworkChange = async () => {
        const networkState = await Network.getNetworkStateAsync();
        if (!networkState.isInternetReachable) {
          ToastAPI.warning(t("network.disconnected"));
        }
      };

      // 立即执行首次检查
      handleNetworkChange();

      // 添加监听（使用新版事件监听方式）
      const subscription = Network.addNetworkStateListener(handleNetworkChange);

      return () => subscription.remove();
    };

    checkNetworkStatus();
  }, [t]);
  useEffect(() => {
    const setupNotifications = async () => {
      await requestNotificationPermission();
      await configureNotificationChannel();
    };
    setupNotifications();
    scheduleLocalNotification();
  }, []);
  return (
    <Provider store={store}>
      <PersistGate persistor={persistor}>
        <AntdProvider>
          <StatusBar
            translucent
            backgroundColor={"#000"}
            barStyle={"light-content"}
          />
          <ActionSheetProvider>
            <GestureHandlerRootView style={{ flex: 1 }}>
              <Stack
                screenOptions={{
                  headerShown: false,
                  animation: "slide_from_right",
                }}
              >
                <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
              </Stack>
            </GestureHandlerRootView>
          </ActionSheetProvider>
        </AntdProvider>
      </PersistGate>
    </Provider>
  );
}

export default withToast(RootLayout);
